﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    using Relation = SampleRelation;

    public static class SampleRelationExtension
    {
        /// <summary>
        /// 根据 ID 获得的一项关联信息
        /// </summary>
        /// <param name="tal"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Relation getRelation(this Relation relation, CoreLimsContext dbContext, string Id) =>
            dbContext.Relation.FirstOrDefault(m => m.Id == Id);

        /// <summary>
        /// 根据源表名和源记录ID，获得源纪录对应关系项
        /// </summary>
        /// <param name="relation"></param>
        /// <param name="dbContext"></param>
        /// <param name="SourceTabName"></param>
        /// <param name="SourceId"></param>
        /// <returns></returns>
        public static string[] GetIncludedIdString(this SampleRelation relation, CoreLimsContext dbContext, string SourceTabName, string IncludedTabName, string SourceId)
        {
            var list = dbContext.Relation.Where(s => s.SourceTabName == SourceTabName && s.IncludedTabName == IncludedTabName && s.SourceId == SourceId)
                .Select(s => new { s.IncludedId })
                .ToArray();

            string[] result = new string[list.Length];
            for (int i = 0; i < list.Length; i++)
            {
                result[i] = list[i].IncludedId;
            }
            return result;
        }

        /// <summary>
        /// 更新检测次数
        /// </summary>
        /// <param name="tal"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="AcceptanceRules"></param>
        /// <returns></returns>
        public static string UpdateRepeat(this SampleRelation relation, CoreLimsContext dbContext, string Id, string Repeat)
        {
            int count = 0;
            int.TryParse(Repeat, out count);

            if (count <= 0)
                return "检测次数必须是大于零的整数！";

            Relation rel = SAMPLE.Relation.getRelation(dbContext, Id);
            rel.Repeat = count;

            dbContext.Update(rel);
            dbContext.SaveChanges();
            return "更新成功！";
        }

        /// <summary>
        /// 更新默认指标集
        /// </summary>
        /// <param name="relation"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="Repeat"></param>
        /// <returns></returns>
        public static string UpdateSampleSpecCollectionIsDefault(this SampleRelation relation, CoreLimsContext dbContext, string Id)
        {
            Relation rel = SAMPLE.Relation.getRelation(dbContext, Id);
            var list = dbContext.Relation.Where(s => s.SourceId == rel.SourceId && s.SourceTabName == "SampleDefinition"
                && s.IncludedTabName == "SpecCollection" && s.IncludedId != rel.IncludedId).ToList();

            foreach (Relation item in list)
            {
                item.Repeat = null;
                dbContext.Update(item);
            }

            rel.Repeat = 1;
            dbContext.Update(rel);
            dbContext.SaveChanges();
            return "更新成功！";
        }
    }
}
